package com.edu.dao.impl;

import com.edu.dao.BaseDao;
import com.edu.dao.addGoodsDao;
import com.edu.entity.Goods;
import com.edu.entity.Goodsimg;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public  class addGoodsDaoimpl implements addGoodsDao {
    
    BaseDao bd = new BaseDao();
    
    @Override
    public int addGoods(Goods goods) {
        // SQL插入语句
        String sql = "INSERT INTO goods (goodname, goodprice, goodoriprice, goodtype, " +
                     "goodnum, gooddec, goodimg, goodstat, goodFreightType, " +
                     "goodTradetype, sellerId, goodonsellstat) " +
                     "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            // 获取数据库连接
            conn = bd.getConn();
            // 指定需要返回生成的主键
            pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            
            // 设置参数
            pstmt.setString(1, goods.getGoodname());
            pstmt.setDouble(2, goods.getGoodprice());
            pstmt.setDouble(3, goods.getOriprive());
            pstmt.setInt(4, goods.getGoodtype());
            pstmt.setInt(5, goods.getGoodnum());
            pstmt.setString(6, goods.getGooddec());
            pstmt.setString(7, goods.getGoodimg());
            pstmt.setInt(8, goods.getGoodstat());
            pstmt.setInt(9, goods.getGoodFreightType());
            pstmt.setInt(10, goods.getGoodTradetType());
            pstmt.setInt(11, goods.getSellerId());
            pstmt.setInt(12, goods.getGoodOnSellStat());
            
            // 执行更新
            int rowsAffected = pstmt.executeUpdate();
            if (rowsAffected > 0) {
                // 获取生成的主键
                rs = pstmt.getGeneratedKeys();
                if (rs.next()) {
                    return rs.getInt(1); // 返回生成的商品ID
                }
            }
            return -1;
        } catch (SQLException e) {
            System.err.println("添加商品到数据库失败: " + e.getMessage());
            e.printStackTrace();
            return -1;
        } finally {
            // 确保资源被正确关闭
            bd.closeAll(conn, pstmt, rs);
        }
    }
    
    @Override
    public boolean addGoodsimg(Goodsimg goodsimg) {
        String sql = "INSERT INTO goodsimg (img1url, img2url, img3url, img4url, goodsid) " +
                    "VALUES (?, ?, ?, ?, ?)";
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = bd.getConn();
            pstmt = conn.prepareStatement(sql);
            
            pstmt.setString(1, goodsimg.getImg1url());
            pstmt.setString(2, goodsimg.getImg2url());
            pstmt.setString(3, goodsimg.getImg3url());
            pstmt.setString(4, goodsimg.getImg4url());
            pstmt.setInt(5, goodsimg.getGoodsid());
            
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            bd.closeAll(conn, pstmt);
        }
    }
    
    public Goods getGoodsById(int goodsId) {
        String sql = "SELECT * FROM goods WHERE goodid = ?";
        try (Connection conn = bd.getConn();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, goodsId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    Goods goods = new Goods();
                    goods.setGoodid(goodsId);
                    goods.setGoodname(rs.getString("goodname"));
                    goods.setGoodprice(rs.getDouble("goodprice"));
                    goods.setOriprive(rs.getDouble("goodoriprice"));
                    goods.setGoodnum(rs.getInt("goodnum"));
                    goods.setGooddec(rs.getString("gooddec"));
                    goods.setGoodstat(rs.getInt("goodstat"));
                    goods.setGoodFreightType(rs.getInt("goodFreightType"));
                    goods.setGoodTradetType(rs.getInt("goodTradetype"));
                    return goods;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<String> getGoodsImages(int goodsId) {
        List<String> images = new ArrayList<>();
        String sql = "SELECT img1url, img2url, img3url, img4url FROM goodsimg WHERE goodsid = ?";
        try (Connection conn = bd.getConn();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, goodsId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    for (int i = 1; i <= 4; i++) {
                        String imgUrl = rs.getString("img" + i + "url");
                        if (imgUrl != null && !imgUrl.isEmpty()) {
                            images.add(imgUrl);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return images;
    }
    public boolean updateGoods(Goods goods) {
        String sql = "UPDATE goods SET goodname = ?, goodprice = ?, goodoriprice = ?, "
                   + "goodtype = ?, goodnum = ?, gooddec = ?, goodimg = ?, goodstat = ?, "
                   + "goodFreightType = ?, goodTradetype = ? WHERE goodsid = ?";
        
        try (Connection conn = bd.getConn();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, goods.getGoodname());
            pstmt.setDouble(2, goods.getGoodprice());
            pstmt.setDouble(3, goods.getOriprive());
            pstmt.setInt(4, goods.getGoodtype());
            pstmt.setInt(5, goods.getGoodnum());
            pstmt.setString(6, goods.getGooddec());
            pstmt.setString(7, goods.getGoodimg());
            pstmt.setInt(8, goods.getGoodstat());
            pstmt.setInt(9, goods.getGoodFreightType());
            pstmt.setInt(10, goods.getGoodTradetType());
            pstmt.setInt(11, goods.getGoodid());
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public boolean updateGoodsimg(Goodsimg goodsimg) {
        String sql = "UPDATE goodsimg SET img1url = ?, img2url = ?, img3url = ?, img4url = ? "
                   + "WHERE goodsid = ?";
        
        try (Connection conn = bd.getConn();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, goodsimg.getImg1url());
            pstmt.setString(2, goodsimg.getImg2url());
            pstmt.setString(3, goodsimg.getImg3url());
            pstmt.setString(4, goodsimg.getImg4url());
            pstmt.setInt(5, goodsimg.getGoodsid());
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
